﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;

namespace ConsoleApp9
{
    public class LoraRequestInfo : IFixedHeaderRequestInfo
    {
        /// <summary>
        /// 接口实现，标识数据长度
        /// </summary>
        public int BodyLength { get; private set; }

        /// <summary>
        /// 自定义属性，标识指令类型
        /// </summary>
        public byte OrderType { get; set; }

        public string MeterNo { get; set; }

        /// <summary>
        /// 自定义属性，标识实际数据
        /// </summary>
        public byte[] Body { get; set; }


        public bool OnParsingBody(byte[] body)
        {
            if (body.Length == this.BodyLength)
            {
                this.Body = body;
                return true;
            }
            return false;

        }

        public bool OnParsingHeader(byte[] header)
        {
            //在该示例中，第一个字节表示后续的所有数据长度，但是header设置的是3，所以后续还应当接收length-2个长度。
            this.BodyLength = header[7];
            this.MeterNo = BitConverter.ToString(header, 0, 6).Replace("-", "");
            this.OrderType = header[6];
            return true;
        }

    }

    public class LoraHeaderCustomDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<LoraRequestInfo>
    {
        /// <summary>
        /// 接口实现，指示固定包头长度
        /// </summary>
        public override int HeaderLength => 8;

        /// <summary>
        /// 获取新实例
        /// </summary>
        /// <returns></returns>
        protected override LoraRequestInfo GetInstance()
        {
            return new LoraRequestInfo();
        }
    }
}
